package com.idealighter.game.games.fqzs.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.dictionary.domain.SharkRoomDomain;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.games.fqzs.SharkPlayer;
import com.idealighter.game.games.fqzs.message.ReqApplyBankerMsg;
import com.idealighter.game.games.fqzs.message.ResRestStageMsg;
import com.idealighter.game.message.ModuleMsgIdConstant;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.context.ApplicationContext;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.game.robot.scheduler.PlayerScheduler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(ModuleMsgIdConstant.Fqzs.RES_REST_STAGE)
public class ResRestStageHandler implements ResMessageHandler {

  private static final Logger LOG = LoggerFactory.getLogger(ResRestStageHandler.class);


  @Override
  public void action(Player player, ResMessage message) {
    ResRestStageMsg msg = (ResRestStageMsg) message;
    MemInfo member = player.member(player.playerId);

    long maxGold = player.configDomain.getMaxGold();

    SharkRoomDomain roomDom = ((SharkPlayer) player).roomDomain;
    // 超过最大携带金币需要退出房间
    if (convertGold(roomDom, member.getChips()) > maxGold) {
      LOG.info("[飞禽走兽]玩家[{}][{}]身上的金币超过最大携带金币", player.userName, player.playerId, maxGold);
      player.delayLogout();
      return;
    }

    // 超过最大游戏局数
    if (player.totalGameNum-- < 1) {
      LOG.info("[飞禽走兽]玩家[{}][{}]超过总游戏局数退出游戏...", player.userName, player.playerId);
      player.delayLogout();
      return;
    }
    SharkPlayer sharkPlayer = (SharkPlayer) player;
    if (member.getChips() >= sharkPlayer.bankerMinChips) {
      int robotBankerApplyCount = robotApplyBankers(player.applyBankers);
      if (robotBankerApplyCount < sharkPlayer.maxRobotApplyBankers) {
        // 机器人上庄
        if (!player.applyBankers.contains(player.playerId)) {
          int restTime = msg.getTime();
          player.schedule(() -> applyBanker(player), RandCodeUtil.random(1, restTime),
              TimeUnit.SECONDS);
        }
      }
    }
  }

  // 申请坐庄
  private void applyBanker(Player player) {
    SharkPlayer sharkPlayer = (SharkPlayer) player;
    int robotBankerApplyCount = robotApplyBankers(player.applyBankers);
    if (robotBankerApplyCount < sharkPlayer.maxRobotApplyBankers) {
      // 机器人上庄
      if (!player.applyBankers.contains(player.playerId)) {
        if (player.bankerId == 0) {
          doApplyBanker(player);
        } else {
          int rand = RandCodeUtil.random(100);
          if (rand <= sharkPlayer.applyBankerProb) {
            doApplyBanker(player);
          }
        }
      }
    }
  }

  // 执行申请上庄
  private void doApplyBanker(Player player) {
    ReqApplyBankerMsg msg = new ReqApplyBankerMsg();
    player.sendMsg(msg);
  }

  private int robotApplyBankers(List<Long> applys) {
    PlayerScheduler playerScheduler = ApplicationContext.getBean(PlayerScheduler.class);
    int result = 0;
    if (applys != null) {
      for (Long apply : applys) {
        if (playerScheduler.isRobot(apply)) {
          result += 1;
        }
      }
    }
    return result;
  }

  /**
   * 筹码换算金币.
   * 
   * @param roomDomain .
   * @param chips .
   */
  public long convertGold(SharkRoomDomain roomDomain, long chips) {
    return chips * roomDomain.getProportionGold() / roomDomain.getProportionChips();
  }
}
